# -*- coding: utf-8 -*-
"""
Created on Fri Oct 21 21:54:32 2022

@author: LUO
"""
from PyQt5.QtCore import QObject
import yaml

class TestCfgDB(QObject):

   NONE, SAMPLE, REF_POS, REF_NEG, REF_BLANK=0,1,2,3,4

   def __init__(self):
      super().__init__()
      self.sampleTypeList = [0]*8
      self.measureJobList = []
      self.measureJobRepeat={}
      self.measureJobRepeat['startIndex'] = 3
      self.measureJobRepeat['stopIndex'] = 5
      self.measureJobRepeat['repeatTimes'] = 5
      self.TempHotCover=105.0
      self.MeltingCurve1 = {}
      self.MeltingCurve1['enabled'] = False
      self.MeltingCurve2 = {}
      self.MeltingCurve2['enabled'] = False

      self.MeasureParameter={}
      self.MeasureParameter['Temp1Err'] = 0.2
      self.MeasureParameter['Temp2Err'] = 0.2
      self.MeasureParameter['Move1M'] = 7
      self.MeasureParameter['Move1N'] = 8
      self.MeasureParameter['Move2M'] = 7
      self.MeasureParameter['Move2N'] = 8

   def NewMeasureJob(self, index, temp=70.0, time=2000, enable_probe1=False, enable_probe2=False):
      testJob={}
      testJob['index'] = index
      testJob['temp'] = temp
      testJob['time'] = time
      testJob['probe1'] = enable_probe1
      testJob['probe2'] = enable_probe2
      self.measureJobList.append(testJob)

   def SetSampelType(self, sampleIndex, sampleType):
      if sampleIndex < 8:
         self.sampleTypeList[sampleIndex] = sampleType

   def SetMelting1Temp(self, enabled, tempStart, tempStop):
      self.MeltingCurve1['enabled'] = enabled
      self.MeltingCurve1['tempStart'] = tempStart
      self.MeltingCurve1['tempStop'] = tempStop

   def SetMelting2Temp(self, enabled, tempStart, tempStop):
      self.MeltingCurve2['enabled'] = enabled
      self.MeltingCurve2['tempStart'] = tempStart
      self.MeltingCurve2['tempStop'] = tempStop

   def SetHotCover(self, temp):
      self.TempHotCover = temp

   def SetMeasureRepeat(self, startIndex=0, stopIndex=0, repeatTimes=0):
      self.measureJobRepeat['startIndex'] = startIndex
      self.measureJobRepeat['stopIndex'] = stopIndex
      self.measureJobRepeat['repeatTimes'] = repeatTimes

   def SetMeasureParameter(self, Temp1Err, Temp2Err, Move1M, Move1N, Move2M, Move2N):
      self.MeasureParameter['Temp1Err'] = Temp1Err
      self.MeasureParameter['Temp2Err'] = Temp2Err
      self.MeasureParameter['Move1M'] = Move1M
      self.MeasureParameter['Move1N'] = Move1N
      self.MeasureParameter['Move2M'] = Move2M
      self.MeasureParameter['Move2N'] = Move2N

   def PrintCfg(self):
      print("sample cfg:", self.sampleTypeList)
      print("hot cover cfg: ", self.TempHotCover)
      print("Melting curve 1: ", self.MeltingCurve1)
      print("Melting curve 2: ", self.MeltingCurve2)
      print("Measure Job: ", self.measureJobList)
      print("Measure Cycles:", self.measureJobRepeat)
      print("meaaure parameter", self.MeasureParameter)

   def GenerateYaml(self):
      py_object ={'sampleCfg': self.sampleTypeList,
                  'hotCoverTemp':self.TempHotCover,
                  'meltingCurve1':self.MeltingCurve1,
                  'meltingCurve2':self.MeltingCurve2,
                  'measurePara':self.MeasureParameter,
                  'measureJobs':self.measureJobList,
                  'measureRepeat':self.measureJobRepeat,
                  }

      print(py_object)
      file = open('./config.yaml', 'w')
      yaml.dump(py_object,file)
      file.close()

   def LoadYaml(self, file_name='./config.yaml'):
      try:
         with open(file_name, 'r') as file:
            cfg = yaml.safe_load(file.read())
            file.close()
         self.sampleTypeList = cfg['sampleCfg']
         self.TempHotCover = cfg['hotCoverTemp']
         self.MeltingCurve1 = cfg['meltingCurve1']
         self.MeltingCurve2 = cfg['meltingCurve2']
         self.MeasureParameter = cfg['measurePara']
         self.measureJobList = cfg['measureJobs']
         self.measureJobRepeat = cfg['measureRepeat']
         return True
      except:
         return False

if __name__ == "__main__":
   db=TestCfgDB()

   #db.GenerateYaml()
   db.LoadYaml()
   db.PrintCfg()